public static void main(String[] args) {
        int[] arr =  {2, 2, 2, 9, 7, 7, 7, 3, 3, 3, 6, 6, 6, 0, 0, 0};
        int len = arr.length;
        char[][] kRadix = new char[len][];

        //创建输入对象
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();

        //定义数组第二维的长度
        int maxLen = 0;
        for (int i = 0; i < len; ++i) {
            //求每个数字的三进制字符串并将其翻转，转为字符数组
            kRadix[i] = new StringBuilder(Integer.toString(arr[i], k)).reverse().toString().toCharArray();
            if (kRadix[i].length > maxLen) {
                maxLen = kRadix[i].length;
            }
        }
        //
        int[] resArr = new int[maxLen];
        for (int i = 0; i < len; ++i) {
            //不进位加法
            for (int j = 0; j < maxLen; ++j) {
                if (j >= kRadix[i].length)
                    resArr[j] += 0;
                else
                    resArr[j] += (kRadix[i][j] - '0');
            }
        }
        int res = 0;
        for (int i = 0; i < maxLen; ++i) {
            res += (resArr[i] % k) * (int)(Math.pow(k, i));
        }
        System.out.println(res);
    }